<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Address extends Controller_User_Template{

	public function action_index()
	{
		$view = new View('address/add');
		
		$order = $this->request->post('order');
		$total = $this->request->post('total');
		
		$view->set('order', $order);
		$view->set('total', $total);

		$this->template->content = $view;
	}
	
	public function action_add()
	{	
		$loggedin = Auth::instance()->get_user();
		$user_id = $loggedin->id;
		$address = new Model_Address();
		$address->values($this->request->post());
		$address->user_id = Auth::instance()->get_user()->id;
		
		if($address->billing)
		{
			$addresses = DB::update('addresses')
				->where('user_id', '=', $user_id)
				->where('billing', '=' , '1')
				->set(array('billing' => '0'))
				->execute();
		}
		
		if($address->shipping)
		{
			$addresses = DB::update('addresses')
				->where('user_id', '=', $user_id)
				->where('shipping', '=' , '1')
				->set(array('shipping' => '0'))
				->execute();
		}
		
		$address->save();		
		
		$session = Session::instance();
		
		$session->set('order', $this->request->post('order'));
		$session->set('total', $this->request->post('total'));
		
		$this->request->redirect('cart/payment');
		
	}
}